package com.zx.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

public class DateUtils {
	
    /** 年-月-日 时:分:秒 显示格式 */
    // 备注:如果使用大写HH标识使用24小时显示格式,如果使用小写hh就表示使用12小时制格式。
    public static String DATE_TO_STRING_DETAIAL_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /** 年-月-日 显示格式 */
    public static String DATE_TO_STRING_SHORT_PATTERN = "yyyy-MM-dd";
    
    /**
     * 
     * 字符串转换为对应日期(可能会报错异常)
     * 
     * @param source
     * @param pattern
     * @return
     */
    public static Date stringToDate(String source, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = simpleDateFormat.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    
    /**
     * 获取当前时间
     * @return
     */
    public static Date getCurrentTime() {
        return Calendar.getInstance().getTime();
    }
    
	/**
	 * 获取UTC时间
	 * @param d
	 * @return
	 */
    public static Date getUTCTime(Date d) {
		
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
        int dstOffset = cal.get(Calendar.DST_OFFSET);
        cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));

        return cal.getTime();
    }
	
	/**
	 * 获取服务器时区偏移量（毫秒）
	 * @return
	 */
    public static int getSYSTimeOffset() {
		
        Calendar cal = Calendar.getInstance();
        int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
        int dstOffset = cal.get(Calendar.DST_OFFSET);

        return (zoneOffset + dstOffset);
    }
	
	/**
	 * 获取UTC时间
	 * @param d
	 * @return
	 */
    public static long getUTCTimeInMillis(Date d) {
		
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        int zoneOffset = cal.get(Calendar.ZONE_OFFSET);
        int dstOffset = cal.get(Calendar.DST_OFFSET);
        cal.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));

        return cal.getTimeInMillis();
    }
    /**
     * 供时间戳换算调用，最终获取到本地时间对应的服务器的时间
     * 不同时区，比如+8区的8点的时间戳与 +9区9点的时间戳是一致的
     * @param timeZoneId
     * @param date
     * @return
     * @throws ParseException
     */
    public static long getUTCTime4Millis(String timeZoneId,Date date) throws ParseException{
		TimeZone timezone = TimeZone.getTimeZone(timeZoneId);
		int minllis = timezone.getOffset(date.getTime());
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MILLISECOND, -minllis);//UTC
		
		int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
		int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
		cal.add(Calendar.MILLISECOND, (zoneOffset+dstOffset));
		return cal.getTime().getTime();
	}
    /**
     * 对应的utc时间
     * @param timeZoneId
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getUTCTime(String timeZoneId,Date date) throws ParseException{
  		TimeZone timezone = TimeZone.getTimeZone(timeZoneId);
  		int minllis = timezone.getOffset(date.getTime());
  		
  		Calendar cal = Calendar.getInstance();
  		cal.setTime(date);
  		cal.add(Calendar.MILLISECOND, -minllis);
  		
  		return cal.getTime();
  	}
    
    /**
     * 获取本地零点对应的UTC时间
     * @param timeZoneId
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getUTCDateZero(String timeZoneId,long timeInMillis) throws ParseException{
		TimeZone timezone = TimeZone.getTimeZone(timeZoneId);
		int minllis = timezone.getOffset(timeInMillis);
		Calendar cal = Calendar.getInstance();
		cal.setTime(DateUtils.getUTCTime(timeInMillis));
		cal.add(Calendar.MILLISECOND, minllis);
		
  		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
  		cal.add(Calendar.MILLISECOND, -minllis);
  		return cal.getTime();
  	}
    
    public static int getOffsetMin(String timeZoneId,Date date) throws ParseException{
		TimeZone timezone = TimeZone.getTimeZone(timeZoneId);
		int minllis = timezone.getOffset(date.getTime())/1000/60;
		return minllis;
	}
	
    /**
	 * 格式化日期 长年分 yyyy 短年份 yy 长月份 MM 短月份 M 长日期 dd 短日期 d
	 * 
	 * @param date
	 * @param fmtStr
	 * @return
	 */
	public static String formatDate(Date date, String fmtStr) {
		SimpleDateFormat fmt = new SimpleDateFormat(fmtStr);
		String ret = fmt.format(date);
		return ret;
	}

	static SimpleDateFormat sdf_iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	
	public static Date now() {
		return new Date();
	}
	
	/**
	 * 获取当前时间的UTC毫秒数
	 */
	public static long getUTCTimeInMillis() {
		java.util.Calendar cal = java.util.Calendar.getInstance();
		int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
		int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
		cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
		return cal.getTimeInMillis();
	}
	
	/**
	 * 获取当前UTC日期
	 */
	public static Date getUTCTime() {
		java.util.Calendar cal = java.util.Calendar.getInstance();
		int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
		int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
		cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
		return cal.getTime();
	}
	
	/**
	 * 指定格式utctime
	 */
	public static String getUTCTime(String format) {
		return formatDate(getUTCTime(), format);
	}
	
	public static Date getUTCTime(int ext) {
		java.util.Calendar cal = java.util.Calendar.getInstance();
		cal.setTime(getUTCTime());
		cal.add(Calendar.SECOND, ext);
		/*int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
		int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
		cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));*/
		return cal.getTime();
	}
	
    /**
     * @param date 当地时间
     * @param timeZone 当地时区
     * @return UTC 时间
     */
	public static Date getUTCTime(Date date,int timeZone) {
   		java.util.Calendar cal = java.util.Calendar.getInstance();
   		cal.setTime(date);
   		cal.add(Calendar.HOUR, 0-timeZone);
   		return cal.getTime();
   	}
   
   	/**
   	 * 前台时间转 utc时间戳
   	 * 
   	 * @param date
   	 * @param timeZone
   	 * @return
   	 */
   	public static long getUTCTimeInMillis(Date date,int timeZone) {
   		java.util.Calendar cal = java.util.Calendar.getInstance();
   		cal.setTime(date);
   		//前台时间转utc时间
   		cal.add(Calendar.HOUR, 0-timeZone);
   	
   		int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
   		int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
	
		//+8区的 1970-01-01 08:00:00 ==== UTC的 1970-01-01 00:00:00
		//不同时间的时间戳的起点终点是同时平移的--》不同时区的时间戳是一样的
		//
		cal.add(java.util.Calendar.MILLISECOND, zoneOffset + dstOffset);
	
   		return cal.getTimeInMillis();
   	}
   	
	/**
   	 * 前台时间转 utc时间戳
   	 * 
   	 * @param date
   	 * @param timeZone
   	 * @return
   	 */
   	public static long getUTCTimeInMillis(Date date,String stringId,boolean useDst) {
   		
   		TimeZone t = TimeZone.getTimeZone(stringId);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int offset = t.getRawOffset();
		if(useDst){
			offset = t.getOffset(cal.getTimeInMillis());
		}
		cal.add(Calendar.MILLISECOND, 0-offset);
		/*int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
		int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
		cal.add(Calendar.MILLISECOND, (zoneOffset+dstOffset));*/
//		System.out.println(cal.getTime());
		return cal.getTimeInMillis();
   	}
   	
   	/**
   	 * 获取utc时间
   	 * @param date
   	 * @param stringId
   	 * @param useDst
   	 * @return
   	 */
 	public static Date getUTCTime(Date date,String stringId,boolean useDst) {
   		TimeZone t = TimeZone.getTimeZone(stringId);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		
		int offset = t.getRawOffset();
		if(useDst){
			offset = t.getOffset(cal.getTimeInMillis());
		}
		cal.add(Calendar.MILLISECOND, 0-offset);
		return cal.getTime();
   	}
   
   	public static String getUTCTime(long millis,String fmtStr){
		
		Calendar calendar = Calendar.getInstance();
		
		calendar.setTimeInMillis(millis);
		int zoneOffset = calendar.get(java.util.Calendar.ZONE_OFFSET);
		int dstOffset = calendar.get(java.util.Calendar.DST_OFFSET);
		calendar.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
		return formatDate(calendar.getTime(), fmtStr);
	}
   	
   	public static Date getUTCTime(long millis){
		
		Calendar calendar = Calendar.getInstance();
		
		calendar.setTimeInMillis(millis);
		int zoneOffset = calendar.get(java.util.Calendar.ZONE_OFFSET);
		int dstOffset = calendar.get(java.util.Calendar.DST_OFFSET);
		calendar.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
		return calendar.getTime();
	}
   	
   	
   	/**
   	 * 获取一段时间内 时间戳偏移情况，会出现1-2种偏移量（跨夏令时开始结束点）
   	 * offset_s 			前一段
   	 * interval 			utc间隔时间点
   	 * offset_e 			后一段
   	 * timezone_offset 		默认时区-转毫秒偏移量
   	 * @param startDate
   	 * @param endDate
   	 * @param stringId
   	 * @param useDst
   	 * @return 
   	 * 
   	 */
	public static Map<String,Object> getDayOffset(Date startDate,Date endDate,String stringId,boolean useDst){
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(startDate);
		
		Map<String,Object> map = new HashMap<String, Object>();
		TimeZone t = TimeZone.getTimeZone(stringId);
		map.put("timezone_offset", t.getRawOffset());
		if(useDst){
			int offsetS = t.getOffset(cal.getTimeInMillis());
			map.put("offset_s", offsetS);
			map.put("interval", getUTCTime(endDate, stringId, useDst));
			map.put("offset_e", offsetS);
			while(startDate.before(endDate)){
				cal.add(Calendar.DAY_OF_MONTH, +1);
				if(cal.getTime().after(endDate)){
					break;
				}
				int offsetE = t.getOffset(cal.getTimeInMillis());
				if(offsetE!=offsetS){
					map.put("interval", getUTCTime(cal.getTime(), stringId, useDst));
					map.put("offset_e", offsetE);
					break;
				}
			}
		}else{
			int offsetS = t.getRawOffset();
			map.put("offset_s", offsetS);
			map.put("interval", getUTCTime(endDate, stringId, useDst));
			map.put("offset_e", offsetS);
		}
		return map;
	}
	
	public static String getLocalDataForISO8601_minllis(Date date,int timeInMillis) throws ParseException{
		Calendar cal =Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MILLISECOND, timeInMillis);
		//转成iso 8601格式
		String dateStr = formatDate(cal.getTime(), "yyyy-MM-dd'T'HH:mm:ss")+getTimezone(timeInMillis);
		return dateStr;
	}
	
	/**
	 * 日期转成utc
	 * @param date
	 * @param timeZoneId
	 * @param useDst
	 * @return
	 * @throws ParseException
	 */
	public static Date getUTCData(String date,String timeZoneId,boolean useDst) throws ParseException{
		TimeZone timezone = TimeZone.getTimeZone(timeZoneId);
		
		Calendar cal =Calendar.getInstance();
		cal.setTime(parse(date, "yyyy-MM-dd"));
		if(useDst){
			cal.add(Calendar.MILLISECOND, 0-timezone.getOffset(cal.getTimeInMillis()));
		}else {
			cal.add(Calendar.MILLISECOND, 0-timezone.getRawOffset());
		}
		
		return cal.getTime();
	}
	
	/**
	 * 将时间戳转成iso8601格式
	 * @param offsetS
	 * @param intervalDate
	 * @param offsetE
	 * @param millis
	 * @param timezoneoffset
	 * @return
	 */
	public static String getLocalData(int offsetS,Date intervalDate,int offsetE,long millis,int timezoneoffset){
		//先时间戳转utc
		Date date = getUTCTime(millis);
		if(date.before(intervalDate)){
			date = getLocalTimeByOffset(millis, offsetS);
		}else{
			date = getLocalTimeByOffset(millis, offsetE);
		}
		//转成iso 8601格式
		String dateStr = formatDate(date, "yyyy-MM-dd'T'HH:mm:ss")+getTimezone(timezoneoffset);
		return dateStr;
	}
	
	/**
	 * string时间转成iso8601格式时间
	 * @param date
	 * @param timeZone 时区 小时
	 * @return
	 * @throws ParseException
	 */
	
	public static int getMinllis(String date,String timeZoneId,boolean useDst) throws ParseException{
		TimeZone timezone = TimeZone.getTimeZone(timeZoneId);
		
		Calendar cal =Calendar.getInstance();
		cal.setTime(parse(date, "yyyy-MM-dd"));
		int minllis = 0;
		if(useDst){
			minllis = timezone.getOffset(cal.getTimeInMillis());
		}else {
			minllis = timezone.getOffset(cal.getTimeInMillis());
		}
		return minllis;
	}
	
	public static String getLocalDataForISO8601(String baseDate,String timeZoneId,boolean useDst,String utcDate) throws ParseException{
		TimeZone timezone = TimeZone.getTimeZone(timeZoneId);
		
		Calendar cal =Calendar.getInstance();
		cal.setTime(parse(baseDate, "yyyy-MM-dd"));
		int minllis = timezone.getRawOffset();
		if(useDst){
			minllis = timezone.getOffset(cal.getTimeInMillis());
		}
		cal.setTime(parse(utcDate, "yyyy-MM-dd HH:mm:ss"));
		cal.add(Calendar.MILLISECOND, minllis);
		return getLocalDataForISO8601_minllis(cal.getTime(), minllis);
	}
	
	public static Date getLocalData(String timeZoneId,boolean useDst,long timeInMillis) throws ParseException{
		TimeZone timezone = TimeZone.getTimeZone(timeZoneId);
		int minllis = timezone.getRawOffset();
		if(useDst){
			minllis = timezone.getOffset(timeInMillis);
		}
		Date date = getUTCTime(timeInMillis);
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MILLISECOND, minllis);
		return cal.getTime();
	}
	
	/**
	 * 根据时间戳获取本地时间
	 * @param timeZoneId
	 * @param timeInMillis
	 * @return
	 * @throws ParseException
	 */
	public static Date getLocalData(String timeZoneId ,long timeInMillis) throws ParseException{
		TimeZone timezone = TimeZone.getTimeZone(timeZoneId);
		int minllis = timezone.getOffset(timeInMillis);
		Calendar cal = Calendar.getInstance();
		cal.setTime(DateUtils.getUTCTime(timeInMillis));
		cal.add(Calendar.MILLISECOND, minllis);
		return cal.getTime();
	}
	
	/**
	 * 根据UTC时间获取本地时间
	 * @param timeZoneId
	 * @param utcDate
	 * @return
	 * @author wangchangchun
	 * @create 2016-12-1 下午12:44:29
	 */
	public static Date getLocalData(String timeZoneId ,Date utcDate) {
		TimeZone timezone = TimeZone.getTimeZone(timeZoneId);
		int minllis = timezone.getOffset(utcDate.getTime());
		Calendar cal = Calendar.getInstance();
		cal.setTime(utcDate);
		cal.add(Calendar.MILLISECOND, minllis);
		return cal.getTime();
	}
	
	public static long getLocaleMillis(String timeZoneId ,long millis) {
		/*Calendar calendar = Calendar.getInstance();
		
		calendar.setTimeInMillis(millis);
		int zoneOffset = calendar.get(java.util.Calendar.ZONE_OFFSET);
		int dstOffset = calendar.get(java.util.Calendar.DST_OFFSET);
		calendar.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
		millis = millis -(zoneOffset + dstOffset);
		
		TimeZone timezone = TimeZone.getTimeZone(timeZoneId);
		int minllis = timezone.getOffset(millis);
		millis = millis+minllis;*/
		return millis;
	}
	/**
	 * 获取本地时间对应的服务器时间戳，依据时间戳特性，将本地时间转换成对应的服务器时间
	 * 比如+8区的08时间戳==+9区的09时间戳
	 * @param day
	 * @param timezoneId
	 * @return
	 * @throws ParseException 
	 */
	public static long getServerTimeMillis(String day,String timezoneId) throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getUTCTime(parse(day, "yyyy-MM-dd"),timezoneId,true));
		cal.setTime(getDate(cal.getTime(), 1));
		int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
		int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
		cal.add(java.util.Calendar.MILLISECOND, zoneOffset + dstOffset);
		return cal.getTimeInMillis();
	}
	
	public static String getTimezone(int timezoneoffset){
		String dateStr  = "";
		if(timezoneoffset==0){
			dateStr +="Z";
		}else{
			if(timezoneoffset>0){
				dateStr +="+";
			}else{
				dateStr +="-";
			}
			timezoneoffset =  Math.abs(timezoneoffset)/1000;
			int hour = timezoneoffset/3600;
			String hourStr = "";
			if(hour<10){
				hourStr = "0"+String.valueOf(hour);
			}else{
				hourStr = String.valueOf(hour);
			}
			int min = timezoneoffset%3600/60;
			String minStr = "";
			if(min<10){
				minStr = "0"+String.valueOf(min);
			}else{
				minStr = String.valueOf(min);
			}
			if(timezoneoffset>0){
				
			}
			dateStr += hourStr+":"+minStr;
		}
		return dateStr;
	}
	
	public static String parseISO8601(String date,boolean toUtc){
		String dateStr  = null;
		
		if(date.length()>=20){
			String timeZone = date.substring(19);
			
			
			date = date.substring(0, 19);
			try{
				Integer offset = getOffset(timeZone);
				if(offset==null){
					return null;
				}
				Calendar cal = Calendar.getInstance();
				cal.setTime(sdf_iso8601.parse(date));
				if(toUtc){
					cal.add(Calendar.MILLISECOND, -offset);
				}
				dateStr = formatDate(cal.getTime(), "yyyy-MM-dd HH:mm:ss");
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		return dateStr;
	}
   
    /**
     * 获取当地时间
     * @param date
     * @param timeZone
     * @return
     */
   	public static Date getLocalTime(int timeZone) {
	   long time = getUTCTimeInMillis();
	   Calendar cal = Calendar.getInstance();
	   cal.setTimeInMillis(time);
	   cal.add(Calendar.HOUR, 0+timeZone);
	   return cal.getTime();
   }
   	
   	/**
     * 获取当地时间
     * @param date
     * @param timeZone
     * @return
     */
   	public static Date getLocalTime(long time,int timeZone) {
	   Calendar cal = Calendar.getInstance();
	   cal.setTimeInMillis(time);
	   int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
	   int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
	   cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
	   cal.add(Calendar.HOUR, 0+timeZone);
	   return cal.getTime();
   }
   	
	public static Date getLocalTimeByOffset(long time,int offset) {
		   Calendar cal = Calendar.getInstance();
		   cal.setTimeInMillis(time);
		   int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
		   int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
		   cal.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
		   cal.add(Calendar.MILLISECOND, 0+offset);
		   return cal.getTime();
	}
	
	public static int getOffset(int timeZone) {
		  return timeZone*60*60*1000;
	 }
	
	public static Integer getOffset(String timeZone) {
		Integer offset = null;
		if(!timeZone.startsWith("Z")){
			String sign = timeZone.substring(0, 1);
			timeZone = timeZone.substring(1);
			String[] str = timeZone.split(":");
			if(str.length==2){
				offset = Integer.parseInt(str[0])*60*60*1000+ Integer.parseInt(str[1])*60*1000;
				if(sign.equals("-")){
					offset = -offset;
				}
			}
		}else{
			offset = 0;
		}
		return offset;
	}
   
	/**
	 * 查询当月的天数
	 * 
	 * @param date
	 * @return
	 */
	public static int getDaysAmountByMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		calendar.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		calendar.roll(Calendar.DATE, -1);// 日期回滚一天，也就是最后一天
		return calendar.get(Calendar.DATE);
	}

	/**
	 * 获取一年的天数
	 * 
	 * @param date
	 * @return
	 */
	public static int getDaysAmountByYear(Date date) {
		int year = Integer.parseInt(formatDate(date, "yyyy"));
		int days = 365;
		if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {// 判断是否闰年，闰年366天
			days = 366;
		}
		return days;
	}


	/**
	 * 把月份转换成英文简写
	 * 
	 * @param month
	 *            jan:1 月份
	 * @return
	 */
	public static String formatMonthEnglish(Integer month) {
		switch (month) {
		case 1: {
			return "Jan";
		}
		case 2: {
			return "Feb";
		}
		case 3: {
			return "Mar";
		}
		case 4: {
			return "Apr";
		}
		case 5: {
			return "May";
		}
		case 6: {
			return "Jun";
		}
		case 7: {
			return "Jul";
		}
		case 8: {
			return "Aug";
		}
		case 9: {
			return "Sep";
		}
		case 10: {
			return "Oct";
		}
		case 11: {
			return "Nov";
		}
		case 12: {
			return "Dec";
		}
		default: {
			return null;
		}
		}
	}

	/**
	 * @param week
	 * @return
	 */
	public static String formatWeekEnglish(Integer week) {
		switch (week) {
		case 1: {
			return "Sun";
		}
		case 2: {
			return "Mon";
		}
		case 3: {
			return "Tue";
		}
		case 4: {
			return "Wed";
		}
		case 5: {
			return "Thu";
		}
		case 6: {
			return "Fri";
		}
		case 7: {
			return "Sat";
		}
		default:
			return null;
		}
	}
	
	/**
	 * 获取前 n 天 的 天列表
	 * 
	 * @param dateTime
	 *            yyyy-MM-dd
	 * @param days
	 *            前 n 天
	 * @return yyyy-MM-dd
	 */
	public static List<String> getDayList(Date dateTime, int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateTime);

		calendar.add(Calendar.DATE, 0 - days);
		Date startDate = calendar.getTime();
		List<String> dayList = new ArrayList<String>();
		dayList.add(formatDate(startDate, "yyyy-MM-dd"));
		for (int i = 0; i < days - 1; i++) {
			calendar.setTime(startDate);
			calendar.add(Calendar.DATE, 1);
			startDate = calendar.getTime();
			dayList.add(formatDate(startDate, "yyyy-MM-dd"));
		}
		return dayList;
	}

	/**
	 * 获取前n月 列表
	 * 
	 * @param dateTime
	 *            yyyy-MM
	 * @param months
	 * @return yyyy-MM
	 */
	public static List<String> getMonthList(Date dateTime, int months) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateTime);

		calendar.add(Calendar.MONTH, 0 - months);
		Date startDate = calendar.getTime();
		List<String> monthList = new ArrayList<String>();
		monthList.add(formatDate(startDate, "yyyy-MM"));
		for (int i = 0; i < months - 1; i++) {
			calendar.setTime(startDate);
			calendar.add(Calendar.MONTH, 1);
			startDate = calendar.getTime();
			monthList.add(formatDate(startDate, "yyyy-MM"));
		}
		return monthList;
	}
	
	/**
	 * 获取指定日期前/后N天的日期， 默认+1天
	 * @param date	指定日期
	 * @param day	N天
	 * @return
	 */
	public static Date getDate(Date date, Integer day){
		day = day == null || day == 0 ? 1 : day;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, day);
		return calendar.getTime();
	}
	
	/**
	 * 获取指定日期前/后N月的日期， 默认+1月
	 * @param date	指定日期
	 * @param num	N月
	 * @return
	 */
	public static Date getDateAddMonth(Date date, Integer num){
		num = num == null || num == 0 ? 1 : num;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, num);
		return calendar.getTime();
	}
	
	/**
	 * 获取指定日期前/后N年的日期， 默认+1年
	 * @param date	指定日期
	 * @param num	N年
	 * @return
	 */
	public static Date getDateAddYear(Date date, Integer num){
		num = num == null || num == 0 ? 1 : num;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, num);
		return calendar.getTime();
	}
	
	/**
	 * 获取指定日期前/后N天的日期， 默认+1天
	 * @param date	指定日期
	 * @param day	N天
	 * @return
	 */
	public static Date getDateForYear(Date date, Integer year){
		year = year == null || year == 0 ? 1 : year;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		
		calendar.add(Calendar.YEAR, year);
		return calendar.getTime();
	}
		
	public static Date parse(String date, String format) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.parse(date);
	}
	
	public static Integer getHour(Timestamp time) {
		Date date = new Date();
		date = time;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	public static Integer getDay(Timestamp time) {
		Date date = new Date();
		date = time;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public static Integer getDay(Date time) {
		Date date = new Date();
		date = time;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public static Integer getMonth(Date time) {
		Date date = new Date();
		date = time;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH) + 1;
	}

	public static Integer getYear(Date time) {
		Date date = new Date();
		date = time;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.YEAR);
	}
	
	public static boolean isSameHour(Date date){
		String nowhour = formatDate(getUTCTime(), "yyyy-MM-dd HH");
		String lasthour = formatDate(date, "yyyy-MM-dd HH");
		if(nowhour.equals(lasthour)){
			return true;
		}else{
			return false;
		}
	}
	public static boolean isSameDay(Date date){
		String nowday = formatDate(getUTCTime(), "yyyy-MM-dd");
		String lastday = formatDate(date, "yyyy-MM-dd");
		
		if(nowday.equals(lastday)){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 判断时间段的开始结束时间 间隔是否大于7天
	 * @param startDate
	 * @param endDate
	 * @return
	 * @throws ParseException 
	 */
	public static boolean is(String startDate,String endDate) throws ParseException{
		Date start = parse(startDate, "yyyy-MM-dd");
		Date end = parse(startDate, "yyyy-MM-dd");
		
		//后面一天传入的是0时刻，数据需要包含这一条 
		if(end.getTime() - start.getTime() <= 6*24*60*60*1000){
			return true;
		}else {
			return false;
		}
	}
	
	public static boolean isSameDate(Date lastUtcDate ,String timezoneId){
		boolean flag = false;
		if(lastUtcDate==null){
			return flag;
		}
		Calendar lc = Calendar.getInstance();
		lc.setTime(getLocalData(timezoneId, lastUtcDate));
		
		Calendar nc = Calendar.getInstance();
		nc.setTime(getLocalData(timezoneId, getUTCTime()));
		
		if(nc.get(Calendar.DAY_OF_YEAR)==lc.get(Calendar.DAY_OF_YEAR)){
			flag = true;
		}
		return flag;
		
	}
	
	public static boolean timezoneExist(String timeZone){
		if(timeZone.equals("Etc/GMT+12")){
			return true;
		}
		if(timeZone.equals("Etc/GMT+11")){
			return true;
		}
		if(timeZone.equals("HST")){
			return true;
		}
		if(timeZone.equals("Pacific/Marquesas")){
			return true;
		}
		if(timeZone.equals("AST")){
			return true;
		}
		if(timeZone.equals("PST")){
			return true;
		}
		if(timeZone.equals("Mexico/BajaNorte")){
			return true;
		}
		if(timeZone.equals("America/Chihuahua")){
			return true;
		}
		if(timeZone.equals("MST")){
			return true;
		}
		if(timeZone.equals("US/Arizona")){
			return true;
		}
		if(timeZone.equals("America/Mexico_City")){
			return true;
		}
		if(timeZone.equals("Canada/Saskatchewan")){
			return true;
		}
		if(timeZone.equals("CST")){
			return true;
		}
		if(timeZone.equals("US/Central")){
			return true;
		}
		if(timeZone.equals("America/Bogota")){
			return true;
		}
		if(timeZone.equals("EST")){
			return true;
		}
		if(timeZone.equals("US/East-Indiana")){
			return true;
		}
		if(timeZone.equals("America/Caracas")){
			return true;
		}
		if(timeZone.equals("Canada/Atlantic")){
			return true;
		}
		if(timeZone.equals("America/Cuiaba")){
			return true;
		}
		if(timeZone.equals("America/La_Paz")){
			return true;
		}
		if(timeZone.equals("America/Asuncion")){
			return true;
		}
		if(timeZone.equals("Canada/Newfoundland")){
			return true;
		}
		if(timeZone.equals("BET")){
			return true;
		}
		if(timeZone.equals("America/Argentina/Buenos_Aires")){
			return true;
		}
		if(timeZone.equals("America/Cayenne")){
			return true;
		}
		if(timeZone.equals("America/Montevideo")){
			return true;
		}
		if(timeZone.equals("America/Santiago")){
			return true;
		}
		if(timeZone.equals("Etc/GMT+2")){
			return true;
		}
		if(timeZone.equals("Atlantic/Cape_Verde")){
			return true;
		}
		if(timeZone.equals("Atlantic/Azores")){
			return true;
		}
		if(timeZone.equals("Europe/Dublin")){
			return true;
		}
		if(timeZone.equals("Africa/Casablanca")){
			return true;
		}
		if(timeZone.equals("Africa/Monrovia")){
			return true;
		}
		if(timeZone.equals("UTC")){
			return true;
		}
		if(timeZone.equals("Europe/Amsterdam")){
			return true;
		}
		if(timeZone.equals("Europe/Belgrade")){
			return true;
		}
		if(timeZone.equals("Europe/Brussels")){
			return true;
		}
		if(timeZone.equals("Europe/Sarajevo")){
			return true;
		}
		if(timeZone.equals("Africa/Windhoek")){
			return true;
		}
		if(timeZone.equals("Africa/Bangui")){
			return true;
		}
		if(timeZone.equals("Asia/Beirut")){
			return true;
		}
		if(timeZone.equals("Asia/Damascus")){
			return true;
		}
		if(timeZone.equals("ART")){
			return true;
		}
		if(timeZone.equals("Africa/Harare")){
			return true;
		}
		if(timeZone.equals("Europe/Helsinki")){
			return true;
		}
		if(timeZone.equals("Africa/Cairo")){
			return true;
		}
		if(timeZone.equals("Europe/Athens")){
			return true;
		}
		if(timeZone.equals("Asia/Jerusalem")){
			return true;
		}
		if(timeZone.equals("Europe/Istanbul")){
			return true;
		}
		if(timeZone.equals("Asia/Baghdad")){
			return true;
		}
		if(timeZone.equals("Asia/Kuwait")){
			return true;
		}
		if(timeZone.equals("Europe/Minsk")){
			return true;
		}
		if(timeZone.equals("Europe/Moscow")){
			return true;
		}
		if(timeZone.equals("Africa/Nairobi")){
			return true;
		}
		if(timeZone.equals("Asia/Tehran")){
			return true;
		}
		if(timeZone.equals("Asia/Muscat")){
			return true;
		}
		if(timeZone.equals("Asia/Yerevan")){
			return true;
		}
		if(timeZone.equals("Asia/Baku")){
			return true;
		}
		if(timeZone.equals("Asia/Tbilisi")){
			return true;
		}
		if(timeZone.equals("Indian/Mauritius")){
			return true;
		}
		if(timeZone.equals("Europe/Moscow")){
			return true;
		}
		if(timeZone.equals("Asia/Kabul")){
			return true;
		}
		if(timeZone.equals("Asia/Ashgabat")){
			return true;
		}
		if(timeZone.equals("Asia/Yekaterinburg")){
			return true;
		}
		if(timeZone.equals("Asia/Karachi")){
			return true;
		}
		if(timeZone.equals("Asia/Calcutta")){
			return true;
		}
		if(timeZone.equals("Asia/Kathmandu")){
			return true;
		}
		if(timeZone.equals("Asia/Dacca")){
			return true;
		}
		if(timeZone.equals("Asia/Novosibirsk")){
			return true;
		}
		if(timeZone.equals("Asia/Rangoon")){
			return true;
		}
		if(timeZone.equals("Asia/Krasnoyarsk")){
			return true;
		}
		if(timeZone.equals("Asia/Bangkok")){
			return true;
		}
		if(timeZone.equals("PRC")){
			return true;
		}
		if(timeZone.equals("Asia/Singapore")){
			return true;
		}
		if(timeZone.equals("Australia/Perth")){
			return true;
		}
		if(timeZone.equals("Asia/Taipei")){
			return true;
		}
		if(timeZone.equals("Asia/Ulaanbaatar")){
			return true;
		}
		if(timeZone.equals("Asia/Irkutsk")){
			return true;
		}
		if(timeZone.equals("Asia/Pyongyang")){
			return true;
		}
		if(timeZone.equals("Australia/Eucla")){
			return true;
		}
		if(timeZone.equals("Asia/Tokyo")){
			return true;
		}
		if(timeZone.equals("Asia/Seoul")){
			return true;
		}
		if(timeZone.equals("Asia/Irkutsk")){
			return true;
		}
		if(timeZone.equals("Australia/Adelaide")){
			return true;
		}
		if(timeZone.equals("Australia/Darwin")){
			return true;
		}
		if(timeZone.equals("Australia/Brisbane")){
			return true;
		}
		if(timeZone.equals("Asia/Vladivostok")){
			return true;
		}
		if(timeZone.equals("Pacific/Guam")){
			return true;
		}
		if(timeZone.equals("Australia/Hobart")){
			return true;
		}
		if(timeZone.equals("Australia/Canberra")){
			return true;
		}
		if(timeZone.equals("Australia/Lord_Howe")){
			return true;
		}
		if(timeZone.equals("Asia/Magadan")){
			return true;
		}
		if(timeZone.equals("SST")){
			return true;
		}
		if(timeZone.equals("Pacific/Norfolk")){
			return true;
		}
		if(timeZone.equals("Asia/Anadyr")){
			return true;
		}
		if(timeZone.equals("Pacific/Auckland")){
			return true;
		}
		if(timeZone.equals("Pacific/Fiji")){
			return true;
		}
		if(timeZone.equals("Etc/GMT-12")){
			return true;
		}
		if(timeZone.equals("Pacific/Chatham")){
			return true;
		}
		if(timeZone.equals("MIT")){
			return true;
		}
		if(timeZone.equals("Pacific/Kiritimati")){
			return true;
		}
		return false;
	}
	
	public static Date getStartWeek(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int i = cal.get(Calendar.DAY_OF_WEEK);
		if(i==1){
			i=7;
		}else{
			i = i-1;
		}
		cal.add(Calendar.DAY_OF_MONTH, 1-i);
		return cal.getTime();
	}
	
	public static Date getEndWeek(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int i = cal.get(Calendar.DAY_OF_WEEK);
		if(i==1){
			i=7;
		}else{
			i = i-1;
		}
		cal.add(Calendar.DAY_OF_MONTH, 7-i);
		Date end = cal.getTime();
		
//		if(end.before(new Date())){
			date= end;
//		}
		
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, 1);
		date = cal.getTime();
		return date;
	}
	
	/**
	 * 获取本地时间和当前时间的年差
	 * @param end
	 * @return
	 */
	public static int getYearDiffer(Date end){
		if(end==null){
			return 20;
		}
		Date now = getUTCTime();
		Calendar cal = Calendar.getInstance();
		cal.setTime(end);
		int year =  cal.get(Calendar.YEAR);
		
		cal.setTime(now);
		year = year - cal.get(Calendar.YEAR);
		if(year<0)year = 0;
		return year;
	}
	
	public static boolean checkExpire(Date d ,int type){
		boolean flag = false;
		if(d==null){
			return false;
		}
		if(getUTCTime().before(d)){
			flag = true;
		}
		return flag;
	}
	
	public static boolean checkExpire(Date d,long expire){
		boolean flag = false;
		if(d==null || getUTCTime().getTime()-d.getTime() > expire){
			flag = true;
		}
		return flag;
		
	}
	
	
}
